package com.pfizer.XmlContentHandler;

import static com.pfizer.Entity.Survey.SURVEYBGTIMESTRING;
import static com.pfizer.Entity.Survey.SURVEYCLASSSTRING;
import static com.pfizer.Entity.Survey.SURVEYEDTIMESTRING;
import static com.pfizer.Entity.Survey.SURVEYIDSTRING;
import static com.pfizer.Entity.Survey.SURVEYLUTIMESTRING;
import static com.pfizer.Entity.Survey.SURVEYQNSTRING;
import static com.pfizer.Entity.Survey.SURVEYREMARKSTRING;
import static com.pfizer.Entity.Survey.SURVEYSTRING;
import static com.pfizer.Entity.Survey.SURVEYSUBJSTRING;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.pfizer.Entity.Survey;

/**
 * SurveyContentHandler--<b>解析問卷調查XML數據</b> 本類用於完成解析XML信息，將解析后的XML放於list中供外部訪問
 * 
 * @author krui
 * @version 1.0 2011/11/21
 */
public class SurveyContentHandler extends DefaultHandler {
	private List<Survey> surveys;

	private Survey survey;
	private StringBuilder sb = new StringBuilder();

	/**
	 * 解析XML數據放于List中，此方法用於外部獲得解析后的數據
	 * 
	 * @return List<Survey>
	 */
	public List<Survey> getSurveys() {
		return surveys;
	}

	/**
	 * 在根標籤開始時就實例化list用於存放每組數據
	 */

	@Override
	public void startDocument() throws SAXException {
		surveys = new ArrayList<Survey>();
	}

	/**
	 * 開始解析數據，將得到的數據放於javabean
	 * 
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		if(ch[0]=='\n'&&length==1)
		{
			return;
		}
		String sTmp = new String(ch, start, length);
		sTmp=sTmp.trim();
		if(sTmp.length()==0)
		{
			return;
		}
		// (2)不管在startElement到endElement的过程中，执行了多少次characters，
		// 都会将内容添加到StringBuilder中，不会丢失内容
		sb.append(sTmp);
	}

	/**
	 * 在一個tag開始時實例化組對象，
	 */
	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		if (SURVEYSTRING.equals(localName)) {
			survey = new Survey();
		}
	}

	/**
	 * 在一個tag結束后將本組數據放於list
	 */

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		if (SURVEYSTRING.equals(localName) && survey != null) {
			surveys.add(survey);
			survey = null;
		} else {
			if (survey != null) {
				String valueString = sb.toString();
				if (SURVEYIDSTRING.equals(localName)) {
					survey.setID(valueString);
				} else if (SURVEYSUBJSTRING.equals(localName)) {
					survey.setSubject(valueString);
				} else if (SURVEYBGTIMESTRING.equals(localName)) {
					survey.setBgTime(valueString);
				} else if (SURVEYEDTIMESTRING.equals(localName)) {
					survey.setEdTime(valueString);
				} else if (SURVEYREMARKSTRING.equals(localName)) {
					survey.setRemark(valueString);
				} else if (SURVEYCLASSSTRING.equals(localName)) {
					survey.setSurveyClass(valueString);
				} else if (SURVEYQNSTRING.equals(localName)) {
					survey.setQuestionnaire(valueString);
				} else if (SURVEYLUTIMESTRING.equals(localName)) {
					survey.setLstUpdTime(valueString);
				}
			}
		}
		sb.delete( 0, sb.length());
	}
}
